package org.example.bookshelf_manager.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.bookshelf_manager.constant.Myconstants;
import org.example.bookshelf_manager.model.UserInfo;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName LoginInterceptor
 * @Description 拦截器
 * @Author ZJX
 * @Date 2024/8/31 20:35
 * @Version 1.0
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("LoginInterceptor preHandle.....");
        HttpSession session = request.getSession();
         UserInfo userInfo = (UserInfo) session.getAttribute(Myconstants.USER_SESSION_KEY);
        if (userInfo == null || userInfo.getId() <= 0){
            response.setStatus(401);
            return false;
        }
        return true;
    }

//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        log.info("LoginInterceptor postHandle");
//        //        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        log.info("LoginInterceptor afterHandle");
////        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
//    }
}
